package com.jade.store.wechat.service.impl;

import com.jade.bss.base.customer.Customer;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.store.wechat.dao.WechatMsgDao;
import com.jade.store.wechat.dao.WechatMsgTempleteDao;
import com.jade.store.wechat.entity.WechatMsg;
import com.jade.store.wechat.entity.WechatMsgTemplete;
import com.jade.store.wechat.service.WechatMsgTempleteFacade;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created with IntelliJ IDEA.
 * User: JinYa 2009_jinya@163.com
 * Date: 2016-5-5
 * Time: 10:53
 * Version : 1.0
 * Remark:
 * History:
 */
@Service("WechatMsgTempleteFacade")
public class WechatMsgTempleteFacadeImpl extends BaseFacadeImpl implements WechatMsgTempleteFacade {
    @Inject
    @Named("WechatMsgTempleteDao")
    private WechatMsgTempleteDao wechatMsgTempleteDao;
    @Inject
    @Named("WechatMsgDao")
    private WechatMsgDao wechatMsgDao;

    @Transactional(propagation= Propagation.REQUIRED)
    public void SendNewMsg(Customer customer, String appid, String wechatId, String... params) {
        WechatMsgTemplete templete=this.getTemplete(customer,WechatMsgTemplete.TYPE_NEW_MSG);
        WechatMsg wechatMsg=new WechatMsg();

    }

    @Transactional(propagation= Propagation.REQUIRED)
    public void SendRechargeInputMsg(Customer customer, String appid, String wechatId, String... params) {

    }

    @Transactional(propagation= Propagation.REQUIRED)
    public void SendRechargeOutputMsg(Customer customer, String appid, String wechatId, String... params) {

    }

    public WechatMsgTemplete getTemplete(Customer customer, int type) {
        Criteria criteria = new Criteria();
        criteria.add(Restrictions.eq("ownerId", customer.getOwnerId()));
        criteria.add(Restrictions.eq("type", type));
        PagingList<WechatMsgTemplete> list = wechatMsgTempleteDao.queryEntity(WechatMsgTemplete.class,criteria);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            Criteria defaultCriteria = new Criteria();
            defaultCriteria.add(Restrictions.eq("isDefault", WechatMsgTemplete.DEFAULT_IS_YES));
            defaultCriteria.add(Restrictions.eq("type", type));
            PagingList<WechatMsgTemplete> defaultList = wechatMsgTempleteDao.queryEntity(WechatMsgTemplete.class,defaultCriteria);
            return defaultList.get(0);
        }
    }
}
